import axios from '@/utils/fetch'
const app = {
  state: {
    sidebar: {
      opened: !+localStorage.getItem('sidebarStatus')
    },
    customerid: null,
    customername: ''
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        localStorage.setItem('sidebarStatus', 1)
      } else {
        localStorage.setItem('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
    },
    SET_CUSTOMERID: (state, id) => {
      state.customerid = id
    },
    SET_CUSTOMERNAME: (state, name) => {
      state.customername = name
    }
  },
  actions: {
    ToggleSideBar({
      commit
    }) {
      commit('TOGGLE_SIDEBAR')
    },
    SetCustomerInfo({
      commit
    }, info) {
      commit("SET_CUSTOMERID", info.customerid)
      commit("SET_CUSTOMERNAME", info.customername)
    },
    // 获取用户信息
    GetCustomerInfo({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        axios.post('/user/getcustomerslist').then(response => {
          if (response.data.values.length) {
            commit("SET_CUSTOMERID", response.data.values[0].value)
            commit("SET_CUSTOMERNAME", response.data.values[0].label)
            resolve(response.data.values[0].value)
          }
          else {
            reject(new Error('can not get customerinfo'))
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export default app